package org.jeecg.modules.travel.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.travel.dto.feedback.AppFeedbackPageDto;
import org.jeecg.modules.travel.dto.feedback.FeedbackAddDto;
import org.jeecg.modules.travel.dto.feedback.FeedbackAnswerDto;
import org.jeecg.modules.travel.dto.feedback.PcFeedbackPageDto;
import org.jeecg.modules.travel.entity.JhFeedback;
import org.jeecg.modules.travel.service.IJhFeedbackService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.travel.vo.feedback.AppFeedbackRecordVo;
import org.jeecg.modules.travel.vo.feedback.JhFeedbackVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 用户反馈表
 * @Author: jeecg-boot
 * @Date:   2025-04-19
 * @Version: V1.0
 */
@Tag(name="用户反馈表")
@RestController
@RequestMapping("/travel/jhFeedback")
@Slf4j
public class JhFeedbackController extends JeecgController<JhFeedback, IJhFeedbackService> {
	@Autowired
	private IJhFeedbackService jhFeedbackService;
	
	/**
	 * 分页列表查询
	 *
	 * @param dto
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户反馈表-分页列表查询")
	@Operation(summary="用户反馈表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JhFeedbackVo>> queryPageList(PcFeedbackPageDto dto, HttpServletRequest req) {
		JhFeedback feedback = new JhFeedback();
		BeanUtils.copyProperties(dto, feedback);
		QueryWrapper<JhFeedback> queryWrapper = QueryGenerator.initQueryWrapper(feedback, req.getParameterMap());
		queryWrapper.lambda().eq(JhFeedback::getDelFlag, CommonConstant.DEL_FLAG_0);
		Page<JhFeedback> page = new Page<>(dto.getPageNo(), dto.getPageSize());
		return Result.OK(jhFeedbackService.queryPageList(page,queryWrapper));
	}
	
	/**
	 *   添加
	 *
	 * @param jhFeedback
	 * @return
	 */
	@AutoLog(value = "用户反馈表-添加")
	@Operation(summary="用户反馈表-添加")
	@RequiresPermissions("org:jh_feedback:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JhFeedback jhFeedback) {
		jhFeedbackService.save(jhFeedback);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jhFeedback
	 * @return
	 */
	@AutoLog(value = "用户反馈表-编辑")
	@Operation(summary="用户反馈表-编辑")
	@RequiresPermissions("org:jh_feedback:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JhFeedback jhFeedback) {
		jhFeedbackService.updateById(jhFeedback);
		return Result.OK("编辑成功!");
	}

	 /**
	  * 	pc回复
	  * @param
	  * @return
	  */
	 @AutoLog(value = "后台回复")
	 @Operation(summary="后台回复")
	 @RequestMapping(value = "/answer", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> answer(@RequestBody FeedbackAnswerDto feedbackDto,HttpServletRequest req) {
		 String userNameByToken = JwtUtil.getUserNameByToken(req);
		 String answer = jhFeedbackService.answer(userNameByToken, feedbackDto);
		 if (StringUtils.isEmpty(answer)) {
			 return Result.OK("编辑成功!");
		 } else {
			 return Result.error(answer);
		 }
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户反馈表-通过id删除")
	@Operation(summary="用户反馈表-通过id删除")
	@RequiresPermissions("org:jh_feedback:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jhFeedbackService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户反馈表-批量删除")
	@Operation(summary="用户反馈表-批量删除")
	@RequiresPermissions("org:jh_feedback:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jhFeedbackService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户反馈表-通过id查询")
	@Operation(summary="用户反馈表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JhFeedback> queryById(@RequestParam(name="id",required=true) String id) {
		JhFeedback jhFeedback = jhFeedbackService.getById(id);
		if(jhFeedback==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jhFeedback);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jhFeedback
    */
    @RequiresPermissions("org:jh_feedback:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JhFeedback jhFeedback) {
        return super.exportXls(request, jhFeedback, JhFeedback.class, "用户反馈表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("org:jh_feedback:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JhFeedback.class);
    }

	 /* =============================== 移动端服务 ============================================ */

	 @Operation(summary="用户反馈表-移动端反馈列表")
	 @GetMapping(value = "/app/appFeedbackPageList")
	 public Result<IPage<AppFeedbackRecordVo>> appFeedbackPageList(AppFeedbackPageDto dto, HttpServletRequest req) {
		 JhFeedback feedback = new JhFeedback();
		 BeanUtils.copyProperties(dto,feedback);
		 String userId = getUserId();
		 QueryWrapper<JhFeedback> queryWrapper = QueryGenerator.initQueryWrapper(feedback, req.getParameterMap());
		 queryWrapper.lambda().eq(JhFeedback::getDelFlag,CommonConstant.DEL_FLAG_0)
				 .eq(StringUtils.isNotBlank(userId),JhFeedback::getUserId,userId);
		 Page<JhFeedback> page = new Page<>(dto.getPageNo(), dto.getPageSize());
		 return Result.OK(jhFeedbackService.appFeedbackPageList(dto,page,queryWrapper));
	 }


	 @AutoLog(value = "用户反馈表-移动端反馈")
	 @Operation(summary="用户反馈表-移动端反馈")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_feedback:coupleBack")
	 @PostMapping(value = "/app/coupleBack")
	 public Result<String> coupleBack(@RequestBody @Valid FeedbackAddDto dto) {
		 dto.setUserId(getUserId());
		 String res = jhFeedbackService.coupleBack(dto);
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("添加成功！");
	 }

}
